package edu.hawaii.halealohacli;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.sensordata.jaxb.SensorData;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Energy-since class that returns the energy used since the date (yyyy-mm-dd) to now.
 * 
 * @author John MacMillan
 * @author Mitchell Kupfer
 * @author Bryson Hori
 * 
 */
public class EnergySince {
  String tower;
  String date;
  WattDepotClient client;

  /**
   * Default constructor for energy-since.
   * 
   * @param tower the tower to query.
   * @param date the starting date.
   * @param client the WattDepot client.
   */
  public EnergySince(String tower, String date, WattDepotClient client) {
    this.tower = tower;
    this.date = date;
    this.client = client;
  }

  /**
   * Simple accessor method to get the value of tower or the tower name.
   * 
   * @return Returns the value of tower or tower name.
   */
  public String getTower() {
    return this.tower;
  }

  /**
   * Simple accessor method to get the value of date.
   * 
   * @return Returns the value of date.
   */
  public String getDate() {
    return this.date;
  }

  /**
   * Runs the actual command.
   * 
   * @throws Exception If error occurs while running method.
   * @return a boolean, true if command is successful.
   */
  public boolean run() throws Exception {
    boolean value = false;
    List<Source> sources = this.client.getSources();
    String sourceName;
    double energy;
    Format format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    Date startDate;
    Date endDate;
    SensorData data;
    int year = Integer.parseInt(date.substring(0, 4));
    int month = Integer.parseInt(date.substring(5, 7));
    int day = Integer.parseInt(date.substring(8, 10));

    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(GregorianCalendar.YEAR, year);
    calendar.set(GregorianCalendar.MONTH, month - 1);
    calendar.set(GregorianCalendar.DAY_OF_MONTH, day);
    calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
    calendar.set(GregorianCalendar.MINUTE, 0);
    calendar.set(GregorianCalendar.SECOND, 0);

    // Creates a start time to find the energy consumed.
    XMLGregorianCalendar startTime =
        DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);

    XMLGregorianCalendar endTime;

    // Formats and get the start date
    startDate = startTime.toGregorianCalendar().getTime();

    // Goes threw all sources looking for the correct tower name.
    for (Source source : sources) {

      sourceName = source.getName();
      // Finds the correct tower name.

      if (sourceName.compareTo(this.tower) == 0) {

        data = this.client.getLatestSensorData(sourceName);
        // Creates the end time to find the energy consumed.
        endTime = data.getTimestamp();
        // Formats and gets the end date
        endDate = endTime.toGregorianCalendar().getTime();
        // gets the energy consumed based on tower name and time.
        energy = client.getEnergyConsumed(sourceName, startTime, endTime, 15);
        // print the corresponding data based on tower name and data received.
        System.out.format("Total energy consumed by %s from %s to %s is: %s kWh\n", sourceName,
            format.format(startDate), format.format(endDate), energy / 1000);

        value = true;
      }
    }

    return value;
  }
}
